<?
namespace App\Services;

use App\Customize\Func;
use App\Exceptions\FireFlyException;
use Illuminate\Support\Facades\Redis;

class CodeService {
    public static function getCode($phone){
        $code = Func::num_random(6);
        Redis::setex('code:'.$phone, 60*5,$code);
        Redis::setex('time:'.$phone, 60*5,time());
        return $code;
    }
    public static function havCode($phone){
        $save_time = Redis::get('time:'.$phone);
        if (!is_null($save_time) && (time() - $save_time) < 60) {
            throw new FireFlyException(403,"不要频繁获取验证码");
        }
    }
    public static function verifyCode($phone,$code){
        $redis_code = Redis::get('code:'.$phone);
        if($code != $redis_code){
            throw new FireFlyException(403,"验证码无效");
        }
    }
    public static function removeCode($phone){
        Redis::del('code:'.$phone);
        Redis::del('time:'.$phone);
    }
}